home *** CD-ROM | disk | FTP | other *** search
- Path: news.spies.com!usenet
- From: Erik Max Francis <max@alcyone.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Derivation and calling virtual functions
- Date: Fri, 29 Mar 1996 14:56:19 -0800
- Organization: Alcyone Systems
- Message-ID: <315C6A93.1A0E8CEE@alcyone.com>
- References: <graphix.828032689@spiff.cc.iastate.edu>
- NNTP-Posting-Host: newton.alcyone.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
-
- Kent A Vander Velden wrote:
-
- > Now, when I call inst.write() it in turn calls Bass::write() which in
- > turn calls Base::func(). Is there a way to instead have it call
- > Derived::func() if the instance is of type Derived? I hoped the
- > virtual keyword would help in this case.
-
- I'm not sure I understand what your problem is.
-
- First problem is that Derive::write is _not_ overriding Base::write, because
- you've defined it differently. (A good compiler will warn you that you're
- hiding a virtual functions in the base class.) You've declared Derived::write
- as int Derived::write(void), whereas Base::write is void Base::write(void).
- (Plus your use C++ comments and semicolons in the wrong place would not even
- let this sucker compile.)
-
- But to address your question, I think your question comes from a
- misunderstanding of how inheritance works. If you want Derived::write to call
- Derived::func, then write it that way. If it is properly defined as a virtual
- function, then Base::write would call Base::func and Derived::write would call
- Derived::func. That's what C++ is for.
-
- --
- Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
- San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
- H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
- Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
- "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
-